package com.dmwork.service.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.dmwork.service.oss.service.FileService;
import com.dmwork.service.oss.util.OssProperties;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private OssProperties ossProperties;

    private String endpoint, keyid, keysecret, bucketname;

    @Override
    public String upload(InputStream inputStream, String module, String originalFilename) {
        OSS ossClient = getOSSClient();

        //构建日期路径
        String folder = new DateTime().toString("yyyy/MM/dd");
        //以随机数为文件名
        String fileName = UUID.randomUUID().toString();
        //获取文件扩展名
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf('.'));
        String key = module + "/" + folder + "/" + fileName + fileExtension;
        ossClient.putObject(bucketname, key, inputStream);
        ossClient.shutdown();
        return "https://" + bucketname + "." + endpoint + "/" + key;
    }

    @Override
    public void fileRemove(String url) {
        OSS ossClient = getOSSClient();
        log.info(url);
        String host = "https://" + bucketname + "." + endpoint + "/";
        log.info(host);
        String objectName = url.substring(host.length());
        log.info(objectName);
        ossClient.deleteObject(bucketname, objectName);
        ossClient.shutdown();
    }

    @Override
    public void fileBatchRemove(List<String> urlList) {
        OSS ossClient = getOSSClient();
        List<String> keys=new ArrayList<>();
        String host = "https://" + bucketname + "." + endpoint + "/";
        //组装objectName列表
        for (String url : urlList) {
            String objectName=url.substring(host.length());
            keys.add(objectName);
        }
        //批量删除
        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketname).withKeys(keys));
        ossClient.shutdown();
    }


    /**
     * 获取OssClient
     *
     * @return
     */
    private OSS getOSSClient() {

        if (StringUtils.isEmpty(endpoint)||StringUtils.isEmpty(keyid)||StringUtils.isEmpty(keysecret)||StringUtils.isEmpty(bucketname)){
            endpoint = ossProperties.getEndpoint();
            keyid = ossProperties.getKeyid();
            keysecret = ossProperties.getKeysecret();
            bucketname = ossProperties.getBucketname();
        }

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret);

        //如果bucket不存在就创建bucket，并赋予公共读权限
        if (!ossClient.doesBucketExist(bucketname)) {
            ossClient.createBucket(bucketname);
            ossClient.setBucketAcl(bucketname, CannedAccessControlList.PublicRead);
        }
        return ossClient;
    }
}
